package gui;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import com.swtdesigner.SwingResourceManager;
import comm.*;

public class Gui extends JFrame {

	private JButton start;
	private JRadioButton player;
	private JRadioButton computer;
	// private Client cl;
	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	int x;
	int y;
	int type = 0;
	JPanel boardbase;
	ImageIcon board = SwingResourceManager.getIcon(Gui.class,
			"/resources/board.gif");
	ImageIcon white = SwingResourceManager.getIcon(Gui.class,
			"/resources/whitetry.gif");
	ImageIcon black = SwingResourceManager.getIcon(Gui.class,
			"/resources/blacktry.gif");
	BoardClick boardClick;
	JLabel cc;
	static boolean isNet = false;
	static boolean isLocal = false;
	static boolean isChessed = false;

	public boolean isNet() {
		return isNet;
	}

	public boolean isLocal() {
		return isLocal;
	}

	public void setColor(byte color) {
		type = color;
		cc = new JLabel();
		if (color == 1)
			cc.setIcon(white);
		else
			cc.setIcon(black);
		cc.setBounds(this.board.getIconWidth() + 30, 160, 120, 30);
		cc.setText("Your Color");
		getContentPane().add(cc);
		getContentPane().repaint();
	}

	public boolean isChessed() {
		return isChessed;
	}

	public void setChessed(boolean f) {
		isChessed = f;
	}

	public int getcol() {
		return x;
	}

	public int getrow() {
		return y;
	}

	public void gameOver(int i) {
		String msg = null;
		switch (i) {
		case Message.COMM_MSG_WIN:
			msg = "Congratulation! you win  the game!";
			break;
		case Message.COMM_MSG_LOSE:
			msg = "Sorry, you lose the game";
			break;
		case Message.COMM_MSG_TIMEOUT:
			msg = "Sorry, you lose for timeout";
			break;

		case Message.COMM_MSG_DRAW:
			msg = "You meet a counterpart!";
			break;
		default:
			break;
		}
		JOptionPane.showMessageDialog(null, msg);
		isNet = false;
		isLocal = false;
		start.setEnabled(true);
	}

	class Board extends JLabel {
		public Board() {
			super();
			this.setIcon(board);
			this.setBounds(0, 0, board.getIconWidth(), board.getIconHeight());
		}
	}

	class Black extends JLabel {
		public Black(int x, int y) {
			super();
			this.setIcon(black);
			this.setBounds(x, y, black.getIconWidth(), black.getIconHeight());
		}
	}

	class White extends JLabel {
		public White(int x, int y) {
			super();
			this.setIcon(white);
			this.setBounds(x, y, white.getIconWidth(), white.getIconHeight());
		}
	}

	public Gui() {
		super();
		init();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					setVisible(true);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame
	 */
	private void init() {
		getContentPane().setLayout(null);
		setBounds(100, 100, this.board.getIconWidth() + 190, this.board
				.getIconHeight() + 35);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		boardbase = new JPanel();
		boardbase.setBounds(0, 0, this.board.getIconWidth(), this.board
				.getIconHeight());
		getContentPane().add(boardbase);
		boardbase.setOpaque(false);
		boardbase.setLayout(null);
		getContentPane().add(new Board());
		// boardbase.addMouseListener(new BoardClick());
		ButtonGroup group = new ButtonGroup();
		start = new JButton();
		start.setText("Start!");
		start.setBounds(this.board.getIconWidth() + 30, 110, 120, 30);
		getContentPane().add(start);
		computer = new JRadioButton();
		computer.setText("C Vs C");
		computer.setBounds(this.board.getIconWidth() + 30, 30, 120, 30);
		player = new JRadioButton();
		player.setText("P Vs C");
		player.setBounds(this.board.getIconWidth() + 30, 70, 120, 30);
		getContentPane().add(computer);
		getContentPane().add(player);
		group.add(computer);
		group.add(player);
		boardClick = new BoardClick();
		start.addMouseListener(new StartClick());
	}

	public void chess(int type, int x, int y) {
		switch (type) {
		case 0:
			boardbase.add(new Black(x * 23, y * 23));
			boardbase.repaint();
			break;

		case 1:
			boardbase.add(new White(x * 23, y * 23));
			boardbase.repaint();
			break;
		}

	}

	class BoardClick extends MouseAdapter {
		public void mousePressed(final MouseEvent e) {
			if (e.getX() > 23 * 14.5 || e.getY() > 23 * 14.5)
				return;
			x = (e.getX() / 23);
			y = (e.getY() / 23);
			isChessed = true;
		}
	}

	class StartClick extends MouseAdapter {
		public void mousePressed(final MouseEvent e) {

			if (start.isEnabled()) {
				boardbase.removeAll();
				boardbase.repaint();
				if (player.isSelected()) {
					boardbase.addMouseListener(boardClick);
					isNet = false;
					isLocal = true;
				}
				if (computer.isSelected()) {
					boardbase.removeMouseListener(boardClick);
					isNet = true;
					isLocal = false;
				}
			}
			start.setEnabled(false);
		}

	}

}
